﻿package mortal.game.view.copy
{
    import Framework.MQ.*;
    import Message.Command.*;
    import Message.DB.Tables.*;
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.utils.*;
    import mortal.common.*;
    import mortal.common.net.*;
    import mortal.component.gconst.*;
    import mortal.game.*;
    import mortal.game.cache.*;
    import mortal.game.control.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.scene3D.ai.*;
    import mortal.game.scene3D.ai.ais.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.utils.*;
    import mortal.game.view.autoArena.*;
    import mortal.game.view.combinedServer.*;
    import mortal.game.view.common.*;
    import mortal.game.view.copy.ChallengeCopy.*;
    import mortal.game.view.copy.GroupTowerCopy.*;
    import mortal.game.view.copy.GroupTowerCopy.data.*;
    import mortal.game.view.copy.QuestionCopy.*;
    import mortal.game.view.copy.ZZXYBattle.*;
    import mortal.game.view.copy.crossMainCity.*;
    import mortal.game.view.copy.defenseCopy.*;
    import mortal.game.view.copy.escort.*;
    import mortal.game.view.copy.expCopy.*;
    import mortal.game.view.copy.groupCopy.*;
    import mortal.game.view.copy.guildCopy.*;
    import mortal.game.view.copy.guildDefenseCopy.*;
    import mortal.game.view.copy.petBreak.*;
    import mortal.game.view.copy.sixBoss.*;
    import mortal.game.view.copy.sunbathCopy.*;
    import mortal.game.view.copy.towerCopy.*;
    import mortal.game.view.copy.treasureHunt.*;
    import mortal.game.view.copy.uniqueBoss.*;
    import mortal.game.view.copy.wine.*;
    import mortal.game.view.copy.worldBoss.*;
    import mortal.game.view.funcPreView.*;
    import mortal.game.view.guide.*;
    import mortal.game.view.guildNew.mercenary.*;
    import mortal.game.view.tianDiRank.*;
    import mortal.mvc.core.*;

    public class CopyController extends Controller
    {
        public var currentCopy:CopyControllerBase;
        private var _copyLeaveBtn:GLoadingButton;
        private var _guideArrow:GuideArrow;
        private var _timerId:int;
        private var _isLeaveCopy:Boolean = false;
        public var escortCopy:EscortController;
        public var guildCopy:GuildCopyController;
        public var defenseCopy:DefenseCopyController;
        public var challengeCopy:ChallengeCopyController;
        public var petBreakCopy:PetBreakCopyController;
        public var towerCopy:TowerCopyController;
        public var groupCopy:GroupCopyController;
        public var worldBoss:WorldBossController;
        public var questionCopy:QuestionCopyController;
        public var sunbathCopy:SunbathCopyController;
        public var winePartyCopy:WinePartyCopyController;
        public var treasureHuntCopy:TreasureHuntController;
        public var ZZXYBattle:ZZXYBattleController;
        public var tianDiRankCopy:TianDiRankController;
        public var guildDefenceCopy:GuildDefenseController;
        public var sixBossCopy:SixBossController;
        public var groupTowerCopy:GroupTowerCopyController;
        public var uniqueBossCopy:UniqueBossController;
        public var autoArenaCopy:AutoArenaController;
        public var heroArenaCopy:HeroArenaController;
        public var mainCityBattle:MainCityBattleController;
        public var arenaRestRoomCopy:ArenaRestRoomController;
        public var expCopy:ExpCopyController;
        public var combinedSer:CombinedSerController;

        public function CopyController()
        {
            this.initControllers();
            return;
        }// end function

        protected function initControllers() : void
        {
            this.escortCopy = new EscortController();
            this.guildCopy = new GuildCopyController();
            this.defenseCopy = new DefenseCopyController();
            this.challengeCopy = new ChallengeCopyController();
            this.petBreakCopy = new PetBreakCopyController();
            this.towerCopy = new TowerCopyController();
            this.groupCopy = new GroupCopyController();
            this.worldBoss = new WorldBossController();
            this.questionCopy = new QuestionCopyController();
            this.sunbathCopy = new SunbathCopyController();
            this.winePartyCopy = new WinePartyCopyController();
            this.treasureHuntCopy = new TreasureHuntController();
            this.ZZXYBattle = new ZZXYBattleController();
            this.tianDiRankCopy = new TianDiRankController();
            this.guildDefenceCopy = new GuildDefenseController();
            this.sixBossCopy = new SixBossController();
            this.groupTowerCopy = new GroupTowerCopyController();
            this.uniqueBossCopy = new UniqueBossController();
            this.autoArenaCopy = new AutoArenaController();
            this.heroArenaCopy = new HeroArenaController();
            this.mainCityBattle = new MainCityBattleController();
            this.arenaRestRoomCopy = new ArenaRestRoomController();
            this.expCopy = new ExpCopyController();
            this.combinedSer = new CombinedSerController();
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicPlayerCopyInfo, this.copyEnterNumHandler);
            Dispatcher.addEventListener(EventName.CopyClickQuickBtn, this.onQuitCopy);
            Dispatcher.addEventListener(EventName.CopyLeave, this.leave);
            Dispatcher.addEventListener(EventName.CopyGroupEnterCopyReq, this.enterGroupCopyReqHandler);
            Dispatcher.addEventListener(EventName.CopyGroupEnterCopyReqOneMore, this.addCopyEnterNum);
            NetDispatcher.addCmdListener(ServerCommand.Copy_EnterCopy, this.onEnterCopy);
            NetDispatcher.addCmdListener(ServerCommand.Copy_LeaveCopy, this.onLeaveCopy);
            NetDispatcher.addCmdListener(ServerCommand.NewDayUpdate, this.newDayUpdate);
            Dispatcher.addEventListener(EventName.Scene_Update, this.sceneUpdateHandler);
            return;
        }// end function

        private function sceneUpdateHandler(event:DataEvent) : void
        {
            this.checkLeaveOrEnterCopy();
            if (this._isLeaveCopy)
            {
                this._isLeaveCopy = false;
                if (cache.role.roleInfo.level < GameConst.LeaveCopyAutoTaskMaxLv)
                {
                    GuideController.isLinkByAutoTask = true;
                    GameController.guide.resumeToTask();
                    GuideController.isLinkByAutoTask = false;
                }
            }
            return;
        }// end function

        private function checkLeaveOrEnterCopy() : void
        {
            var _loc_1:* = Cache.instance.copy.copyInfo;
            var _loc_2:* = _loc_1 != null && Game.isSceneInit() && Game.sceneInfo != null && (Game.sceneInfo.sMapDefine.mapId == _loc_1.intoMapId || _loc_1.maps != null && _loc_1.maps.indexOf(Game.sceneInfo.sMapDefine.mapId.toString()) >= 0);
            if (CopyUtil.isInCopy && _loc_2)
            {
                this.updateCustomShow(cache.copy.copyInfo);
            }
            else if (_loc_2)
            {
                if (MapFileUtil.isInCrossAutofightMap)
                {
                    GameController.guide.showEnterCrossNormalTipsWin();
                    if (!CopyUtil.isInNomalEscortTask)
                    {
                        this.onLeaveCopy(null);
                    }
                }
            }
            return;
        }// end function

        private function copyEnterNumHandler(param1:MessageBlock) : void
        {
            var _loc_2:* = param1.messageBase as SPlayerCopyInfo;
            cache.copy.updateEnterCounts(_loc_2.playerCopys);
            cache.copy.updateExtraEnterCounts(_loc_2.extraGroupNums);
            NetDispatcher.dispatchCmd(ServerCommand.CopyEnterTimesUpdate, null);
            return;
        }// end function

        private function newDayUpdate(param1:Object) : void
        {
            var _loc_2:* = new Dictionary();
            cache.copy.updateExtraEnterCounts(_loc_2);
            if (Cache.instance.copy.groupTower.singleGanInfo)
            {
                Cache.instance.copy.groupTower.singleGanInfo.todayPassNum = 0;
            }
            NetDispatcher.dispatchCmd(ServerCommand.CopyEnterTimesUpdate, null);
            return;
        }// end function

        private function enterGroupCopyReqHandler(event:DataEvent) : void
        {
            var _loc_2:* = event.data as TCopy;
            if (_loc_2 == null)
            {
                return;
            }
            CopyUtil.copyCheck(this.enterCopy, _loc_2);
            return;
        }// end function

        private function enterCopy(param1:TCopy) : void
        {
            GameProxy.copy.enterCopy(param1.code);
            return;
        }// end function

        private function addCopyEnterNum(event:DataEvent) : void
        {
            var _loc_2:* = event.data as TCopy;
            if (_loc_2)
            {
                GameProxy.copy.addCopyNum(_loc_2.code);
            }
            return;
        }// end function

        private function onEnterCopy(param1:Object) : void
        {
            this._isLeaveCopy = false;
            switch(cache.copy.copyInfo.type)
            {
                case ECopyType._ECopyTypeEscort:
                {
                    this.currentCopy = this.escortCopy;
                    break;
                }
                case ECopyType._ECopyTypeGuildBoss:
                {
                    this.currentCopy = this.guildCopy;
                    break;
                }
                case ECopyType._ECopyTypeDefense:
                {
                    this.currentCopy = this.defenseCopy;
                    break;
                }
                case ECopyType._ECopyTypeHeroExpedition:
                {
                    this.currentCopy = this.challengeCopy;
                    break;
                }
                case ECopyType._ECopyTypePetBreak:
                {
                    this.currentCopy = this.petBreakCopy;
                    break;
                }
                case ECopyType._ECopyTypeTower:
                {
                    this.currentCopy = this.groupTowerCopy;
                    break;
                }
                case ECopyType._ECopyTypeTeam:
                {
                    this.currentCopy = this.groupCopy;
                    break;
                }
                case ECopyType._ECopyTypeCross:
                {
                    this.currentCopy = this.worldBoss;
                    break;
                }
                case ECopyType._ECopyTypeSingle:
                {
                    this.currentCopy = this.groupCopy;
                    break;
                }
                case ECopyType._ECopyTypeQuestion:
                {
                    this.currentCopy = this.questionCopy;
                    break;
                }
                case ECopyType._ECopyTypeEarthPlacard:
                case ECopyType._ECopyTypeHeavenPlacard:
                {
                    this.currentCopy = this.tianDiRankCopy;
                    break;
                }
                case ECopyType._ECopyTypeSunbath:
                {
                    this.currentCopy = this.sunbathCopy;
                    break;
                }
                case ECopyType._ECopyTypeGuildWine:
                {
                    this.currentCopy = this.winePartyCopy;
                    break;
                }
                case ECopyType._ECopyTypeVip:
                {
                    this.currentCopy = this.groupCopy;
                    break;
                }
                case ECopyType._ECopyTypePeaceBoss:
                {
                    this.currentCopy = this.ZZXYBattle;
                    break;
                }
                case ECopyType._ECopyTypeGuildDefense:
                {
                    this.currentCopy = this.guildDefenceCopy;
                    break;
                }
                case ECopyType._ECopyTypeRaiders:
                {
                    this.currentCopy = this.treasureHuntCopy;
                    break;
                }
                case ECopyType._ECopyTypeMonsterSiege:
                {
                    this.currentCopy = this.sixBossCopy;
                    break;
                }
                case ECopyType._ECopyTypeVip:
                {
                    this.currentCopy = this.groupCopy;
                    break;
                }
                case ECopyType._ECopyTypeUniqueBoss:
                {
                    this.currentCopy = this.uniqueBossCopy;
                    break;
                }
                case ECopyType._ECopyTypeAutoArena:
                {
                    this.currentCopy = this.autoArenaCopy;
                    break;
                }
                case ECopyType._ECopyTypeArena2v2:
                {
                    this.currentCopy = this.heroArenaCopy;
                    break;
                }
                case ECopyType._ECopyTypeMainCityBattle:
                {
                    this.currentCopy = this.mainCityBattle;
                    break;
                }
                case ECopyType._ECopyTypeMainCityBattel2:
                {
                    this.currentCopy = this.mainCityBattle;
                    break;
                }
                case ECopyType._ECopyTypeArenaBreakRoom:
                {
                    this.currentCopy = this.arenaRestRoomCopy;
                    break;
                }
                case ECopyType._ECopyTypeExp:
                {
                    this.currentCopy = this.expCopy;
                    break;
                }
                default:
                {
                    break;
                }
            }
            if (this.currentCopy)
            {
                this.currentCopy.enterCopy(cache.copy.copyInfo);
            }
            this.checkCanMercenary();
            this.checkLeaveOrEnterCopy();
            return;
        }// end function

        private function checkCanMercenary() : void
        {
            var _loc_1:Array = [GroupTowerCopyUtil.CopyCode];
            if (cache.copy.copyInfo && _loc_1.indexOf(cache.copy.copyInfo.group) != -1)
            {
                GuildNewMercenaryProcessor.canRentCopy = true;
            }
            else
            {
                GuildNewMercenaryProcessor.canRentCopy = false;
            }
            return;
        }// end function

        private function replaceCopyQuickQuit(param1:TCopy) : TCopy
        {
            var _loc_2:int = 0;
            var _loc_3:Array = null;
            var _loc_4:int = 0;
            var _loc_5:String = null;
            var _loc_6:Array = null;
            var _loc_7:TCopy = null;
            if (param1.configByMapInstance != null && Game.sceneInfo != null && Game.sceneInfo.sMapDefine != null && Game.sceneInfo.sMapDefine.instanceType != null)
            {
                _loc_2 = Game.sceneInfo.sMapDefine.instanceType.__value;
                _loc_3 = param1.configByMapInstance.split("#");
                _loc_4 = 0;
                while (_loc_4 < _loc_3.length)
                {
                    
                    _loc_5 = _loc_3[_loc_4];
                    _loc_6 = _loc_5.split(",");
                    if (int(_loc_6[0]) == _loc_2)
                    {
                        _loc_7 = param1;
                        param1 = new TCopy();
                        ClassTypesUtil.copyValue(param1, _loc_7);
                        param1.quickQuit = int(_loc_6[1]);
                        return param1;
                    }
                    _loc_4++;
                }
            }
            return param1;
        }// end function

        private function updateCustomShow(param1:TCopy) : void
        {
            var $info:* = param1;
            var info:* = $info;
            if (info == null)
            {
                info = cache.copy.copyInfo;
            }
            if (info == null)
            {
                return;
            }
            if (FuncPreviewModule.hasInstanceShowing())
            {
                FuncPreviewModule.instance.unExpand();
            }
            info = this.replaceCopyQuickQuit(info);
            if (CopyUtil.needHideTaskTrace(info))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.Wing288IconShowHide, false));
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, false));
            }
            if (CopyUtil.needHideActiveIcons(info))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, false));
            }
            if (this.needHideShortcuts(info))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarShowHide, false));
            }
            if (this.needHideMount(info))
            {
                if (MountUtil.isRideUp)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.MountRide));
                }
            }
            if (this.needHideBestInfo(info))
            {
                cache.best.copyBestViewClose = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.BestViewOpenClose));
            }
            if (CopyUtil.needShowLeaveBtn(info))
            {
                setTimeout(this.addLeaveBtn, 1000);
            }
            else
            {
                this.removeLeaveBtn();
            }
            if (CopyUtil.needAutoFight(info))
            {
                var nextFrame:* = function () : void
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoFight_A));
                return;
            }// end function
            ;
                AIManager.cancelAll();
                CallLater.setCallLater(nextFrame, 1.4);
            }
            if (CopyUtil.needHideActiveIcons(info))
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.FriendBlessBallShowOrHide, false));
            }
            return;
        }// end function

        private function onLeaveCopy(param1:TCopy) : void
        {
            this._isLeaveCopy = true;
            GuildNewMercenaryProcessor.canRentCopy = false;
            if (AutoFightAI.isWorking)
            {
                AIManager.cancelAll();
            }
            if (this.currentCopy)
            {
                this.currentCopy.leaveCopy();
                this.currentCopy = null;
            }
            if (param1 != null && param1.code == GameConst.GuildWarCopyCode)
            {
                GameController.guide.guideExploitBuyHandler();
            }
            if (FuncPreviewUtil.instance.shouldShowModule())
            {
                FuncPreviewModule.instance.expand();
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.ActionIconOpenClose, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.Wing288IconShowHide, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.TaskShowHideTrackExceptBtn, true));
            Dispatcher.dispatchEvent(new DataEvent(EventName.ShortcutBarShowHide, true));
            cache.best.copyBestViewClose = false;
            Dispatcher.dispatchEvent(new DataEvent(EventName.BestViewOpenClose));
            Dispatcher.dispatchEvent(new DataEvent(EventName.FriendBlessBallShowOrHide, true));
            this.removeLeaveBtn();
            Alert.removeAllAlertWin();
            NetDispatcher.dispatchCmd(ServerCommand.BufferUpdate, Cache.instance.buff.showBuffArray);
            if (cache.copy.guideWhenLeaveCopy != -1)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.AutoGuideActiveByEvent, {mainId:cache.copy.guideWhenLeaveCopy, stepId:1}));
                cache.copy.guideWhenLeaveCopy = -1;
            }
            if (this._guideArrow != null)
            {
                this.disposeTimer();
                DisplayUtil.removeMe(this._guideArrow);
                this._guideArrow.dispose(true);
                this._guideArrow = null;
            }
            if (this._copyLeaveBtn)
            {
                this._copyLeaveBtn.filters = [];
            }
            return;
        }// end function

        private function onQuitCopy(event:DataEvent = null) : void
        {
            if (cache.copy.copyInfo == null)
            {
                return;
            }
            if (_view)
            {
                _view.hide();
            }
            if (CopyUtil.needQuitDirectly(cache.copy.copyInfo))
            {
                this.leave();
                return;
            }
            if (cache.copy.copyInfo.type == ECopyType._ECopyTypeCross)
            {
                this.leave();
                return;
            }
            var _loc_2:* = GameDefConfig.instance.getQuitCopyTip(cache.copy.copyInfo.type);
            var _loc_3:* = Language.getString(20197);
            if (_loc_2)
            {
                _loc_3 = _loc_2.text;
                Alert.yesLabel = _loc_2.text1;
                Alert.noLabel = _loc_2.text2;
            }
            Alert.show(_loc_3, null, Alert.YES | Alert.NO, null, this.onLeaveComfirm);
            return;
        }// end function

        public function onLeaveComfirm(param1:int) : void
        {
            if (param1 == Alert.NO)
            {
            }
            else if (param1 == Alert.YES)
            {
                this.leave();
            }
            return;
        }// end function

        private function leave(event:DataEvent = null) : void
        {
            GameProxy.copy.leaveCopy();
            return;
        }// end function

        public function showGuideLeaveArrow(param1:Boolean) : void
        {
            var guideLeaveCopyTimeOut:Function;
            var isShow:* = param1;
            if (isShow)
            {
                guideLeaveCopyTimeOut = function () : void
            {
                _timerId = -1;
                showGuideLeaveArrow(false);
                return;
            }// end function
            ;
                if (this._copyLeaveBtn == null)
                {
                    this.addLeaveBtn();
                }
                if (this._copyLeaveBtn == null)
                {
                    return;
                }
                if (this._guideArrow == null)
                {
                    this._guideArrow = UICompomentPool.getUICompoment(GuideArrow) as GuideArrow;
                }
                this._guideArrow.direction = GuideArrow.Left;
                this._guideArrow.x = this._copyLeaveBtn.x + this._copyLeaveBtn.width + 6;
                this._guideArrow.y = this._copyLeaveBtn.y + this._copyLeaveBtn.height / 2;
                this._guideArrow.label = Language.getString(20995);
                LayerManager.uiLayer.addChild(this._guideArrow);
                this._copyLeaveBtn.filters = [FilterConst.guideFilter];
                this.disposeTimer();
                this._timerId = setTimeout(guideLeaveCopyTimeOut, 18000000);
            }
            else
            {
                this.disposeTimer();
                if (this._guideArrow != null)
                {
                    DisplayUtil.removeMe(this._guideArrow);
                    this._guideArrow.dispose(true);
                    this._guideArrow = null;
                }
                if (this._copyLeaveBtn)
                {
                    this._copyLeaveBtn.filters = [];
                }
            }
            return;
        }// end function

        private function addLeaveBtn(event:Event = null) : void
        {
            var _loc_2:int = 0;
            if (cache.copy.isInCopy && !this._copyLeaveBtn)
            {
                _loc_2 = 20;
                this._copyLeaveBtn = UIFactory.gLoadingButton(ResFileConst.CopyExit, 302, _loc_2, 50, 50, LayerManager.uiLayer);
                this._copyLeaveBtn.name = "copy_LeaveButton";
                this._copyLeaveBtn.configEventListener(MouseEvent.CLICK, this.onLeaveCopyBtnClick);
            }
            return;
        }// end function

        private function removeLeaveBtn(event:Event = null) : void
        {
            if (this._copyLeaveBtn && this._copyLeaveBtn.parent)
            {
                EffectManager.glowFilterUnReg(this._copyLeaveBtn);
                DisplayUtil.removeMe(this._copyLeaveBtn);
                this._copyLeaveBtn = null;
            }
            return;
        }// end function

        private function needHideMainUI(param1:TCopy) : Boolean
        {
            if (!param1)
            {
                return false;
            }
            return false;
        }// end function

        private function needHideShortcuts(param1:TCopy) : Boolean
        {
            return false;
        }// end function

        private function needHideBestInfo(param1:TCopy) : Boolean
        {
            return true;
        }// end function

        private function needHideMount(param1:TCopy) : Boolean
        {
            return param1.type == ECopyType._ECopyTypeDefense || param1.type == ECopyType._ECopyTypeGuildWine || param1.type == ECopyType._ECopyTypeRaiders;
        }// end function

        private function needHideFriendBlessBall(param1:TCopy) : Boolean
        {
            return true;
        }// end function

        private function onLeaveCopyBtnClick(event:MouseEvent) : void
        {
            this.onQuitCopy();
            this.showGuideLeaveArrow(false);
            return;
        }// end function

        private function disposeTimer() : void
        {
            if (this._timerId > 0)
            {
                clearTimeout(this._timerId);
                this._timerId = -1;
            }
            return;
        }// end function

        public function stageResize() : void
        {
            if (this.currentCopy)
            {
                this.currentCopy.stageResize();
            }
            if (this._copyLeaveBtn)
            {
                this._copyLeaveBtn.x = 302;
                this._copyLeaveBtn.y = 20;
            }
            return;
        }// end function

    }
}
